﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Group Student --

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<GroupStudent> GetListStudentByGroupId(int groupId)
        {
            return DataContex != null && groupId > 0
                        ? DataContex.GroupStudents.Where(m => m.IdGroup == groupId).ToList() : null;
        }       
        #endregion

        #region Common

        private static bool GroupStudentCommonValidator(GroupStudent groupstudent, out string message)
        {
            if (groupstudent.IdGroup < 0)
            {
                message = string.Format(ConstantMessages.CanNotFindItem, "Nhóm");
                return false;
            }

            if (groupstudent.IdStudent < 0)
            {
                message = string.Format(ConstantMessages.CanNotFindItem, "Sinh viên");
                return false;
            }  

            message = string.Empty;
            return true;
        }

        #endregion

        #region -- Add Group Student--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupstudent"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddGroupStudentValidator(GroupStudent groupstudent, out string message)
        {
            if (!GroupStudentCommonValidator(groupstudent, out message)) return false;

            if (DataContex.GroupStudents.Any(f => f.IdGroup == groupstudent.IdGroup && f.IdStudent == groupstudent.IdStudent))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "sinh viên thuộc nhóm");
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddGroupStudent(GroupStudent groupstudent)
        {
            DataContex.GroupStudents.AddObject(groupstudent);
            DataContex.SaveChanges();
        }     

        #endregion

        #region -- Edit Group Student --


        public bool EditGroupStudentValidator(GroupStudent groupstudent, out string message)
        {
            var id = groupstudent.Id;

            if (!GroupStudentCommonValidator(groupstudent, out message)) return false;

            if (DataContex.GroupStudents.Any(f => f.IdGroup == groupstudent.IdGroup && f.IdStudent == groupstudent.IdStudent && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "sinh viên ở nhóm");
                return false;
            }
            message = string.Empty;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupstudent"></param>
        /// <returns></returns>
        public bool EditGroupStudent(GroupStudent groupstudent)
        {
            var id = groupstudent.Id;
            var updateGroupStudent = DataContex.GroupStudents.SingleOrDefault(f => f.Id == id);

            if (updateGroupStudent == null)
                return false;

            updateGroupStudent.IdGroup = groupstudent.IdGroup;
            updateGroupStudent.IdStudent = groupstudent.IdStudent;
            updateGroupStudent.IsLeader = groupstudent.IsLeader;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Group  Student--

        public bool DeleteGroupStudentsValidator(IEnumerable<GroupStudent> groups, out string message)
        {
            const string intialize = "Nhóm sinh viên: ";
            var itemsCannotIdentify = (from group1 in groups
                                       let id = group1.Id
                                       where !DataContex.GroupStudents.Any(m => m.Id == id)
                                       select group1).Aggregate(intialize, (current, group1) => current + (group1.IdGroup + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }                 

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteGroupStudents(List<GroupStudent> groups)
        {
            for (var i = groups.Count - 1; i >= 0; i--)
            {
                var id = groups[i].Id;
                var deleteGroup = DataContex.GroupStudents.Single(m => m.Id == id);
                DataContex.GroupStudents.DeleteObject(deleteGroup);
            }
            DataContex.SaveChanges();
        } 
     
        #endregion

    }
}